/*  ------------------------------------------------------------------------
    NQ Common Services Library
    
    Homepage: http://www.awzhome.de/
    ------------------------------------------------------------------------
    
    The contents of this file are subject to the Mozilla Public License
    Version 1.1 (the "License"); you may not use this file except in
    compliance with the License. You may obtain a copy of the License at
    http://www.mozilla.org/MPL/
    
    Software distributed under the License is distributed on an "AS IS"
    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
    License for the specific language governing rights and limitations
    under the License.
   
    The Original Code is code of NQ Common Services Library.

    The Initial Developer of the Original Code is Andreas Weizel.
    Portions created by the Initial Developer are
    Copyright (C) 2006-2010 Andreas Weizel. All Rights Reserved.
    
    Contributor(s): (none)
    
    Alternatively, the contents of this file may be used under the terms
    of the GNU General Public License Version 2.0 or later (the "GPL"),
    or the GNU Lesser General Public License Version 2.1 or later (the
    "LGPL"), in which case the provisions of the GPL or the LGPL are
    applicable instead of those above. If you wish to allow use of your
    version of this file only under the terms of the GPL or the LGPL and
    not to allow others to use your version of this file under the MPL,
    indicate your decision by deleting the provisions above and replace
    them with the notice and other provisions required by the GPL or the
    LGPL. If you do not delete the provisions above, a recipient may use
    your version of this file under the terms of any of the MPL, the GPL
    or the LGPL.
    ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Resources;
using System.Drawing;
using System.Reflection;
using AWZhome.NQ.Core;
using System.IO;
using System.Windows.Media.Imaging;

namespace AWZhome.NQ.CommonServices
{

	[NQExportedService(SingleInstance = true, Category = NQHostMode.GUI)]
	[NQServiceSubstitute(typeof(Services.IResourceService))]
	internal class ResourceGUIService : Services.IResourceGUIService, IDisposable
	{
		private object sync;
		private Dictionary<string, ResourceManager> _mResList = new Dictionary<string, ResourceManager>();
		private Dictionary<string, string> _assemblyNames = new Dictionary<string, string>();

		public ResourceGUIService()
		{
			// Create synchronization object
			sync = NQServiceManager.Instance.SyncObject;

			lock (sync)
			{
				// Iterate through all components and create resource managers for marked assemblies
				foreach (string comp in NQServiceManager.Instance.GetComponentList())
				{
					INQComponentInfo compInfo = NQServiceManager.Instance.GetComponentInfo(comp);
					if ((compInfo.NonTranslatedRes != null) && (!_mResList.ContainsKey(compInfo.NonTranslatedRes)))
					{
						// Non-translated resource has been found
						_mResList.Add(compInfo.NonTranslatedResKey, new ResourceManager(compInfo.NonTranslatedRes, compInfo.MainAssembly));

						// Collect assembly names
						_assemblyNames.Add(compInfo.NonTranslatedResKey, compInfo.MainAssembly.GetName().Name);
					}
					if ((compInfo.TranslatedRes != null) && (!_mResList.ContainsKey(compInfo.TranslatedRes)))
					{
						// Translated resource has been found
						_mResList.Add(compInfo.TranslatedResKey, new ResourceManager(compInfo.TranslatedRes, compInfo.MainAssembly));
					}
				}
			}
		}

		public void Dispose()
		{
			lock (sync)
			{
				// Cleaning up
				_mResList.Clear();
			}
		}

		public string GetResString(string resName, string itemname)
		{
			lock (sync)
			{
				string sResult = null;
				// Get resource string from requested manager
				if (_mResList.ContainsKey(resName))
				{
					sResult = _mResList[resName].GetString(itemname);
				}
				return (sResult != null) ? sResult : String.Empty;
			}
		}

		public string GetResString(string resName, string itemname, params object[] args)
		{
			lock (sync)
			{
				string sFrmString = this.GetResString(resName, itemname);
				return String.Format(sFrmString, args);
			}
		}

		public Bitmap GetResImage(string resName, string itemname)
		{
			lock (sync)
			{
				// Get bitmap resource from requested manager
				if (_mResList.ContainsKey(resName))
				{
					return _mResList[resName].GetObject(itemname) as Bitmap;
				}
				else
				{
					return null;
				}
			}
		}

		public Bitmap GetResImage(string resName, string itemname, Color transparent)
		{
			lock (sync)
			{
				Bitmap resbitmap = this.GetResImage(resName, itemname);
				if (resbitmap != null)
				{
					// Make bitmap color transparent
					resbitmap.MakeTransparent(transparent);
					return resbitmap;
				}
				else
				{
					return null;
				}
			}
		}

		public BitmapImage GetResImageSource(string resName, string itemName)
		{
			lock (sync)
			{
				BitmapImage bitmap = null;
				try
				{
					bitmap = new BitmapImage(new Uri("pack://application:,,,/" + _assemblyNames[resName] + ";component" + itemName));
				}
				catch (Exception e)
				{
					// Log the exception, but leave it catched
					NQServiceManager.Instance.LogMessage(NQLogType.Error, e);
				}
				return bitmap;
			}
		}

		public Bitmap GetResToolbarImage(string resName, string itemname)
		{
			lock (sync)
			{
				return this.GetResImage(resName, itemname, Color.FromArgb(255, 0, 255));
			}
		}

		public Stream GetResStream(string resName, string itemname)
		{
			lock (sync)
			{
				if (_mResList.ContainsKey(resName))
				{
					// Get resource data as binary stream
					return _mResList[resName].GetStream(itemname) as Stream;
				}
				else
				{
					return null;
				}
			}
		}

		public IEnumerable<string> GetResourceNamesEnumerable()
		{
			lock (sync)
			{
				// Return enumeration object (IEnumerable<>) with all resource names
				return (IEnumerable<string>) _mResList.Keys;
			}
		}

	}

}
